﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Models;
using ChoDiaOc_HK_WPF.Controls;

namespace ChoDiaOc_HK_WPF.Views.TiepNhanDangKy {
    /// <summary>
    /// Interaction logic for DangKyQuangCao.xaml
    /// </summary>
    public partial class DangKyQuangCao : UserControl {

        private QC_BANG quangCaoBangSource;

        public QC_BANG QuangCaoBangSource {
            get { return quangCaoBangSource; }
            set { quangCaoBangSource = value; }
        }

        private QC_BAO quangCaoBaoSource;

        public QC_BAO QuangCaoBaoSource {
            get { return quangCaoBaoSource; }
            set { quangCaoBaoSource = value; }
        }

        private BAO baoSource;

        public BAO BaoSource {
            get { return baoSource; }
            set { baoSource = value; }
        }

        private TOBUOM toBuomSource;

        public TOBUOM ToBuomSource {
            get { return toBuomSource; }
            set { toBuomSource = value; }
        }

        private LOAIBANG loaiBangSource;

        public LOAIBANG LoaiBangSource {
            get { return loaiBangSource; }
            set { loaiBangSource = value; }
        }

        private GIATIEN_QC_BANG giaTienQCBangSource;

        public GIATIEN_QC_BANG GiaTienQCBangSource {
            get { return giaTienQCBangSource; }
            set { giaTienQCBangSource = value; }
        }

        private GIATIEN_QC_BAO giaTienQCBaoSource;

        public GIATIEN_QC_BAO GiaTienQCBaoSource {
            get { return giaTienQCBaoSource; }
            set { giaTienQCBaoSource = value; }
        }

        private GIATIEN_TOBUOM giaTienQCBuomSource;

        public GIATIEN_TOBUOM GiaTienQCBuomSource {
            get { return giaTienQCBuomSource; }
            set { giaTienQCBuomSource = value; }
        }

        private List<LOAIBANG> listLoaiBang = null;

        public List<LOAIBANG> ListLoaiBang {
            get {
                if (listLoaiBang == null) {
                    listLoaiBang = DatabaseWrapper.GetInstance().GetAllLoaiBang();
                }
                return listLoaiBang;
            }
            set { listLoaiBang = value; }
        }

        private List<BAO> listBao = null;

        public List<BAO> ListBao {
            get {
                if (listBao == null) {
                    listBao = DatabaseWrapper.GetInstance().GetAllBao();
                }
                return listBao;
            }
            set { listBao = value; }
        }

        private List<BAO> listBaoChon = null;

        public List<BAO> ListBaoChon {
            get {
                if (listBaoChon == null) {
                    listBaoChon = new List<BAO>();
                }
                return listBaoChon;
            }
            set { listBaoChon = value; }
        }

        private NOIDUNGQC noiDungQCSource;

        public NOIDUNGQC NoiDungQCSource {
            get { return noiDungQCSource; }
            set { noiDungQCSource = value; }
        }


        public DangKyQuangCao() {
            InitializeComponent();

            quangCaoBangSource = (QC_BANG)FindResource("quangCaoBangSource");
            quangCaoBaoSource = (QC_BAO)FindResource("quangCaoBaoSource");
            baoSource = (BAO)FindResource("baoSource");
            toBuomSource = (TOBUOM)FindResource("toBuomSource");
            loaiBangSource = (LOAIBANG)FindResource("loaiBangSource");
            giaTienQCBangSource = (GIATIEN_QC_BANG)FindResource("giaTienQCBangSource");
            giaTienQCBaoSource = (GIATIEN_QC_BAO)FindResource("giaTienQCBaoSource");
            giaTienQCBuomSource = (GIATIEN_TOBUOM)FindResource("giaTienQCBuomSource");
            noiDungQCSource = (NOIDUNGQC)FindResource("noiDungQCSource");
            noiDungQCSource.MaND = TiepNhanDangKyController.GetInstance().GenerateMaNOIDUNG();

            TiepNhanDangKyController.GetInstance().QuangCaoBangSource = quangCaoBangSource;
            TiepNhanDangKyController.GetInstance().QuangCaoBaoSource = quangCaoBaoSource;
            TiepNhanDangKyController.GetInstance().BaoSource = baoSource;
            TiepNhanDangKyController.GetInstance().ToBuomSource = toBuomSource;
            TiepNhanDangKyController.GetInstance().LoaiBangSource = loaiBangSource;
            TiepNhanDangKyController.GetInstance().GiaTienQCBangSource = giaTienQCBangSource;
            TiepNhanDangKyController.GetInstance().GiaTienQCBaoSource = giaTienQCBaoSource;
            TiepNhanDangKyController.GetInstance().GiaTienQCBuomSource = giaTienQCBuomSource;
            TiepNhanDangKyController.GetInstance().ListChonBao = ListBaoChon;
            TiepNhanDangKyController.GetInstance().NoiDungQCSource = noiDungQCSource;

            List<String> listTenLoaiBang = ListLoaiBang.Select(t => t.TenLoaiBang).ToList();
            tenLoaiBangComboBox.ItemsSource = listTenLoaiBang;

            List<String> listTenBao = ListBao.Select(t => t.TenBao).ToList();
            tenBaoComboBox.ItemsSource = listTenBao;

            ResetSource();
        }

        public void ResetSource() {

            chonBaoCheckBox.IsEnabled = false;

            //QC_BANG
            dangKyQuangCaoBangCheckBox.IsChecked = false;
            groupBang.IsEnabled = false;

            //QC_BAO
            dangKyQuangCaoBaoCheckBox.IsChecked = false;
            groupBao.IsEnabled = false;
            ngayBatDauPhatHanhDatePicker.SelectedDate = DateTime.Now;

            //QC_BUOM
            dangKyQuangCaoBangCheckBox.IsChecked = false;
            groupDangKyQCBuom.IsEnabled = false;

            btnNhapLai.IsEnabled = true;
            btnChinhSua.IsEnabled = false;
            btnHoanTat.IsEnabled = true;

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            List<int> soLuong = new List<int>();
            int SL = DatabaseWrapper.GetInstance().GetThamSoSoLuongToBuomToiThieu();
            for (int i = 1; i < 21; i++) {
                soLuong.Add(SL * i);
            }

            soLuongTextBox.Items.Clear();
            foreach (int sl in soLuong) {
                soLuongTextBox.Items.Add(sl);
            }

            soLuongTextBox.SelectedIndex = 0;
        }

        private void btnChinhSua_Click(object sender, RoutedEventArgs e) {
            App.ManHinhTiepNhanDangKy.Step = 2;
            App.ManHinhTiepNhanDangKy.DoNextStep(2);
            dangKyQCBuomCheckBox.IsEnabled = dangKyQuangCaoBaoCheckBox.IsEnabled = dangKyQuangCaoBangCheckBox.IsEnabled = true;
            groupBang.IsEnabled = groupBao.IsEnabled = groupDangKyQCBuom.IsEnabled = true;
            noiDungQCTextBox.IsEnabled = true;
            btnChinhSua.IsEnabled = false;
            btnHoanTat.IsEnabled = true;
            btnNhapLai.IsEnabled = true;
        }

        private void btnNhapLai_Click(object sender, RoutedEventArgs e) {
            ResetSource();
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            Validate();
            if (isValidate) {
                App.ManHinhTiepNhanDangKy.DoNextStep(3);
                dangKyQCBuomCheckBox.IsEnabled = dangKyQuangCaoBaoCheckBox.IsEnabled = dangKyQuangCaoBangCheckBox.IsEnabled = false;
                groupBang.IsEnabled = groupBao.IsEnabled = groupDangKyQCBuom.IsEnabled = false;
                noiDungQCTextBox.IsEnabled = false;
                MessageBox.Show("Thông tin hợp lệ!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.None);
                btnChinhSua.IsEnabled = true;
                btnHoanTat.IsEnabled = false;
                btnNhapLai.IsEnabled = false;

                // Sinh ThongTinDiaOc
                noiDungQCSource.ThongTinDiaOc = "Thông tin tổng hợp được đăng trên quảng cáo... (Hậu làm)";

            } else {
                MessageBox.Show("Thông tin không hợp lệ!", "Cảnh báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.None);

            }
        }

        private void btnSinhMaQCBao_Click(object sender, RoutedEventArgs e) {
            maQCBaoTextBox.Text = TiepNhanDangKyController.GetInstance().GenerateMaQC_BAO();
        }

        private void btnSinhMaQCBang_Click(object sender, RoutedEventArgs e) {
            maQCBangTextBox.Text = TiepNhanDangKyController.GetInstance().GenerateMaGIATIEN_QCBANG();
        }

        private void btnSinhMaQCBuom_Click(object sender, RoutedEventArgs e) {
            maToBuomTextBox.Text = TiepNhanDangKyController.GetInstance().GenerateMaGIATIEN_QCTOBUOM();
        }

        private void quangCaoBangCheckBox_Checked(object sender, RoutedEventArgs e) {
            groupBang.IsEnabled = true;
            TiepNhanDangKyController.GetInstance().IsQCBang = true;
        }

        private void quangCaoBangCheckBox_Unchecked(object sender, RoutedEventArgs e) {
            groupBang.IsEnabled = false;
            TiepNhanDangKyController.GetInstance().IsQCBang = false;
        }

        private void tenLoaiBangComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            String selected = (sender as ComboBox).SelectedItem as String;
            TiepNhanDangKyController.GetInstance().LoaiBangSource = loaiBangSource = DatabaseWrapper.GetInstance().GetLoaiBang(selected);
            giaTienLoaiBangTextBox.Text = loaiBangSource.GiaTien.ToString();
        }

        private void dangKyQCBuomCheckBox_Checked(object sender, RoutedEventArgs e) {
            groupDangKyQCBuom.IsEnabled = true;
            TiepNhanDangKyController.GetInstance().IsQCBuom = true;
        }

        private void dangKyQCBuomCheckBox_Unchecked(object sender, RoutedEventArgs e) {
            groupDangKyQCBuom.IsEnabled = false;
            TiepNhanDangKyController.GetInstance().IsQCBuom = false;
        }

        private void dangKyQuangCaoBaoCheckBox_Checked(object sender, RoutedEventArgs e) {
            groupBao.IsEnabled = true;
            TiepNhanDangKyController.GetInstance().IsQCBao = true;
        }

        private void dangKyQuangCaoBaoCheckBox_Unchecked(object sender, RoutedEventArgs e) {
            groupBao.IsEnabled = false;
            TiepNhanDangKyController.GetInstance().IsQCBao = false;
        }

        private void tenBaoComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            chonBaoCheckBox.IsEnabled = true;
            String selected = (sender as ComboBox).SelectedItem as String;
            TiepNhanDangKyController.GetInstance().BaoSource = baoSource = DatabaseWrapper.GetInstance().GetBao(selected);
            thuPhatHanhTextBox.Text = baoSource.ThuPhatHanh;
            giaTienLoaiBaoTextBox.Text = baoSource.GiaTien.ToString();

            if (listBaoChon.Exists(t => t.MaBao == baoSource.MaBao)) {
                chonBaoCheckBox.IsChecked = true;
            } else {
                chonBaoCheckBox.IsChecked = false;
            }
        }

        private void chonBaoCheckBox_Unchecked(object sender, RoutedEventArgs e) {
            if (listBaoChon.Exists(t => t.MaBao == baoSource.MaBao)) {
                listBaoChon.RemoveAll(t => t.MaBao == baoSource.MaBao);
            }
        }

        private void chonBaoCheckBox_Checked(object sender, RoutedEventArgs e) {
            if (!listBaoChon.Exists(t => t.MaBao == baoSource.MaBao)) {
                BAO bao = new BAO();
                bao.MaBao = baoSource.MaBao;
                bao.TenBao = baoSource.TenBao;
                bao.GiaTien = baoSource.GiaTien;
                bao.ThuPhatHanh = baoSource.ThuPhatHanh;
                listBaoChon.Add(bao);
            }
        }

        private bool isValidate = false;

        public bool IsValidate {
            get { return isValidate; }
            set { isValidate = value; }
        }

        private void Validate() {
            if (dangKyQCBuomCheckBox.IsChecked == false && dangKyQuangCaoBangCheckBox.IsChecked == false &&
                (dangKyQuangCaoBaoCheckBox.IsChecked == false || (dangKyQuangCaoBaoCheckBox.IsChecked == true && ListBaoChon.Count == 0))) {

                isValidate = false;
                return;
            }

            if (String.IsNullOrWhiteSpace(quangCaoBangSource.MaQCBang) ||
                String.IsNullOrWhiteSpace(giaTienQCBangSource.MoTa) ||
                giaTienQCBangSource.KichCo <= 0 ||
                String.IsNullOrWhiteSpace(giaTienQCBangSource.DVT) ||
                giaTienQCBangSource.GiaTien <= 0) {

                if (dangKyQuangCaoBangCheckBox.IsChecked == true) {
                    isValidate = false;
                    return;
                }
            }

            if (giaTienQCBuomSource.SoLuong <= 0 || giaTienQCBuomSource.GiaTien <= 0) {
                if (dangKyQCBuomCheckBox.IsChecked == true) {
                    isValidate = false;
                    return;
                }
            }

            if (String.IsNullOrWhiteSpace(noiDungQCTextBox.Text)) {
                isValidate = false;
                return;
            }

            if (String.IsNullOrWhiteSpace(giaTienQCBaoSource.ViTri) || String.IsNullOrWhiteSpace(giaTienQCBaoSource.KhoIn)
            || String.IsNullOrWhiteSpace(giaTienQCBaoSource.MauSac) || String.IsNullOrWhiteSpace(giaTienQCBaoSource.GhiChu) || String.IsNullOrWhiteSpace(quangCaoBaoSource.MaQCBao) ||
            ListLoaiBang.Count == 0) {
                if (dangKyQuangCaoBaoCheckBox.IsChecked == true) {
                    isValidate = false;
                    return;
                }
            }

            isValidate = true;
        }

        private void btnTinhGiaBang_Click(object sender, RoutedEventArgs e) {
            // Cong thuc tinh gia dua tren LOAIBANG, KICHCO, VAT
            giaTienQCBangSource.GiaTien = loaiBangSource.GiaTien * decimal.Parse((giaTienQCBangSource.KichCo * DatabaseWrapper.GetInstance().GetThamSoVAT()).ToString());
            giaTienQCBangSource.MaGiaTien = TiepNhanDangKyController.GetInstance().GenerateMaGIATIEN_QCBANG();
        }

        private void btnTinhGiaBuom_Click(object sender, RoutedEventArgs e) {
            // Cong thuc tinh gia dua tren LOAIBANG, KICHCO, VAT
            giaTienQCBuomSource.GiaTien = decimal.Parse((giaTienQCBuomSource.SoLuong * DatabaseWrapper.GetInstance().GetGiaTien1ToBuom() * DatabaseWrapper.GetInstance().GetThamSoVAT()).ToString());
            giaTienQCBuomSource.MaGiaTien = TiepNhanDangKyController.GetInstance().GenerateMaGIATIEN_QCTOBUOM();
        }

        private void btnTinhGiaBao_Click(object sender, RoutedEventArgs e) {
            // Cong thuc tinh gia dua tren LOAIBANG, KICHCO, VAT
            if (ListBaoChon.Count == 0) {
                return;
            }
            Decimal tienBao = 0.0M;
            foreach (BAO b in ListBaoChon) {
                tienBao += b.GiaTien;
            }
            Double tien = Double.Parse(tienBao.ToString());
            tien *= DatabaseWrapper.GetInstance().GetThamSoVAT();
            giaTienQCBaoSource.GiaTien = decimal.Parse(tien.ToString());
            giaTienQCBaoSource.MaGiaTien = TiepNhanDangKyController.GetInstance().GenerateMaGIATIEN_QCBAO();
        }               
    }

    public class KichCoConverter : IValueConverter {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            
            if (targetType == typeof(Double)){
                int val = (int)value;
                switch (val) {
                    case 1:
                        return "2 x 2";
                    case 2:
                        return "4 x 4";
                    case 3:
                        return "6 x 6";
                    case 4:
                        return "8 x 8";
                    case 5:
                        return "10 x 10"; 
                        
                }
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            String val = value as String;
            switch (val) {
                case "2 x 2":
                    return 1;
                case "4 x 4":
                    return 2;
                case "6 x 6":
                    return 3;
                case "8 x 8":
                    return 4;
                case "10 x 10":
                    return 5;
            }
            return -1;
        }
    }
}
